<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/9/24
  Time: 9:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>学生展示</title>
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" >
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit" >分配权限</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addStu">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getData">获取当前页数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">是否全选</button>
    </div>
</script>
<div id="premissiontree" class="demo-tree demo-tree-box" style="display: none"></div>


<table class="layui-hide" id="test" lay-filter = "test"></table>
<!-- 注意：项目正式环境请勿引用该地址 -->
<script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<%--<script src="${pageContext.request.contextPath}/static/jquery-3.6.1.min.js"></script>--%>
<script>

        layui.use(['table','layer','form','jquery','tree'], function(){
            var tree = layui.tree;
            var table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
             var $ = layui.jquery;
            var namestatus=true;

            var data=[];
            // 发出ajax请求  获取树形菜单需要的数据
            $.get("${pageContext.request.contextPath}/tMenu/treedata",function (d){
                if(d.code==0){
                    data=d.data;
                }

            });

            table.render({
                // element  元素
                elem: '#test'
                ,page:true
                ,limit:8
                ,limits:[8,10,20,30]
                ,toolbar: '#toolbarDemo'
                ,url:'${pageContext.request.contextPath}/tRole' //  表格了里面所需要的数据就是一个list里面有很多的map、对象
                ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{type:'number',title:'编号'}
                    // field 后面跟的内容  必须跟后台传过来的数据里面的map 的key保持一致
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'rname', title: '角色名字', sort: true}
                    ,{fixed: 'right', title:'操作', minWidth: 125, toolbar: '#barDemo'}
                ]]

            });



            // 工具栏事件
            table.on('toolbar(test)', function(obj){
                var uid = obj.config.uid;
                var checkStatus = table.checkStatus(uid);
                var othis = lay(this);
                switch(obj.event){
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(layui.util.escape(JSON.stringify(data)));
                        break;
                    case 'getData':
                        var getData = table.getData(id);
                        console.log(getData);
                        layer.alert(layui.util.escape(JSON.stringify(getData)));
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选')
                        break;
                    case 'addStu':
                        $("#saveUserForm")[0].reset();
                        $(".pass").css("display","block");
                        layer.open({
                            type: 1,
                            area: ['500px', '400px'],
                            offset: '100px',
                            content:$("#premissiontree"),

                        });
                        break;
                };
            });

            //触发单元格工具事件
            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                //console.log(obj)
                if(obj.event === 'edit'){

                    // 1、弹出层
                    layer.open({
                        type: 1,
                        area: ['500px', '400px'],
                        offset: '100px',
                        content:$("#premissiontree"),
                        btn: ['提交']
                        ,yes: function(index, layero){

                                //按钮【按钮一】的回调
                                alert("提交按钮");
                                // 1。 获取当前的角色的id
                            var rid = data.id;
                            // 2. 获取当前的角色选中的菜单的id
                            var mids=[];// 数组
                            var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据  id
                            //[{"id":1,"title":"员工管理","spread":true,"children":[{"id":2,"title":"员工操作","spread":true,"children":[]}]},{"id":6,"title":"角色管理","spread":true,"children":[{"id":8,"title":"测试","spread":true,"children":[]}]},{"id":9,"title":"fpa性格测试","spread":true,"children":[{"id":13,"title":"性格统计","spread":true,"children":[]}]}]
                           // alert("角色的id"+rid+"菜单的数据"+JSON.stringify(checkedData));
                            checkedData.forEach(function(item){
                                //{"id":3,"title":"设置","spread":true,"children":[{"id":4,"title":"个人信息","spread":true,"children":[]}]}
                                //alert(JSON.stringify(item));
                                //{"id":9,"title":"fpa性格测试","spread":true,"children":[{"id":10,"title":"试题管理","spread":true,"children":[]},{"id":11,"title":"做题记录","spread":true,"children":[]},{"id":12,"title":"执行计划","spread":true,"children":[]},{"id":13,"title":"性格统计","spread":true,"children":[]}]}
                                mids.push(item.id);
                                // 二级菜单的id
                                item.children.forEach(function (item2){
                                    mids.push(item2.id);
                                })

                            })
                            //alert(mids.toString());
                            $.post("${pageContext.request.contextPath}/tRoleMenu",{rid:rid,mids:mids.toString()},function (d){

                                if(d.data==true){
                                    layer.closeAll();
                                }
                            })

                            // 提交到后台  修改数据库中的数据
                            // t_role_menu   ① 删除原有的权限  ②添加新的权限


                             }
                        // 查询所有的菜单的数据

                    });
                    //2、给树形菜单填充数据
                    setDataToTree(data.id);

                }
            });


            // ==> function setDataToTree(){}   js
            var setDataToTree = function(rid){

                // alert(JSON.stringify(data));
                tree.render({
                    elem: '#premissiontree'
                    ,data: data
                    ,showCheckbox: true  //是否显示复选框
                    ,id: 'demoId1'
                    //,isJump: true //是否允许点击节点时弹出新窗口跳转
                    ,click: function(obj){
                        var data = obj.data;  //获取当前点击的节点数据
                        layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
                    }
                });
                // 选中当前角色拥有的菜单
                // tree.setChecked('demoId1', [3,4]); //勾选指定节点
                // 根据当前的角色的id 查询一下对应的菜单的id
                $.get('${pageContext.request.contextPath}/tMenu/checkData/'+rid,function(r){
                    if(r.code==0){
                        //alert(JSON.stringify(r.data));
                        tree.setChecked('demoId1', r.data);
                    }
                })
                // 1 是父菜单  一旦选中父菜单  对应的子菜单全部选中

            }














        });




</script>

</body>
</html>
